package mysqlConnecionMappersPackage;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dataConnectionPackage.DataConnector;
import dataConnectionPackage.IMapper;

import dataTypesPackage.Sefer;

public class SeferMapper implements IMapper {
	private ResultSet rs;
	@Override
	public Object get(int ID) {
		Sefer sefer;
		// Gets sefer with given ID
		rs = DataConnector.getInstance().query("select * from `sefer` where `seferID` = `" + ID + "`");
		if (rs != null){
			try {
				rs.next();
				if (Integer.parseInt(rs.getString(9)) == 1 ){
					 sefer= new Sefer(Integer.parseInt(rs.getString(1)),rs.getString(2), Integer.parseInt(rs.getString(3)), Integer.parseInt(rs.getString(4)),rs.getString(5), rs.getDate(6),rs.getDate(7), Integer.parseInt(rs.getString(8)));
					 return sefer;
				}
				else
					System.out.println("you don't have acces to sefer with given ID");
			} catch (NumberFormatException e) {
				System.out.println("SeferMapper: number format problem");
				e.printStackTrace();
			} catch (SQLException e) {
				System.out.println("SeferMapper: sql problem");
				e.printStackTrace();
			}
		}
		else
			System.out.println("no sefer with given ID");
		return null;
	}


	@Override
	public List<Object> getAll() {
		// Gets all sefers, returns as ArrayList
		List<Object> allSeferisList = new ArrayList<Object>();
		rs = DataConnector.getInstance().query("select * from `sefer`;");
        try {
        	if (rs != null){
        		while(rs.next()){
        			int numberOfRows = DataConnector.getInstance().countRows("sefer");
        			Sefer temp;
        			for (int i=1;i<=(numberOfRows*9);i+=9){																		//table has 9 coloums. since we add 9 every loop, 9*row needed
        				temp = new Sefer(Integer.parseInt(rs.getString(i)),rs.getString(i+1),Integer.parseInt(rs.getString(i+2)),Integer.parseInt(rs.getString(i+3)), rs.getString(i+4), rs.getDate(i+5), rs.getDate(i+6),Integer.parseInt(rs.getString(i+7)));
        				allSeferisList.add(temp);
        			}               
        		}
        	}
        }
        catch(Exception e){
        	e.printStackTrace();
        }
		return allSeferisList;
	}
	

	@Override
	public int set(Object object) {
		// records given sefer, returns ID of it
        boolean isUpdated;
        Sefer sefer=(Sefer) object;
		try {
			isUpdated = DataConnector.getInstance().update("INSERT INTO `sefer` " +
//			        "(MusteriID, name, surname, musteriType) " +
//			        "values ('"+(DataConnector.getInstance().countRows("musteri")+1)+"', '"+musteri.getName()+"', '"+musteri.getSurName()+"', '"+musteri.getMusteriType().ordinal()+"')");
			        "(name, gemiID, guzergahID,startDate,endDate) " +
			        "values ('"+sefer.getName()+"', '"+sefer.getGemi().getGemiID()+"', '"+sefer.getGuzergah().getGuzergahID()+"','"+sefer.getStartDate()+"','"+sefer.getEndDate()+"')");
			if (isUpdated)
				return DataConnector.getInstance().countRows("sefer");
		} catch (SQLException e) {
			System.out.println("seferMapper: sql problem");
			e.printStackTrace();
		}

		return -1;
	}

}
